package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNReceiverInfo;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.fragment.MineFragement;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.TimeUtils;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.YNInviteSecret;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import sweetcamera.PailideShowTextActivity;

public class PailideShowActivity extends Activity implements View.OnClickListener {
    private MediaPlayer mMediaPlayer = null;
    private ImageView show_cover;
    private LinearLayout time_pailide;
    private RelativeLayout pailide_show_top;
    private SimpleDraweeView pailide_show_pic;
    private LinearLayout cp_label_text, cp_label_mic, cp_label_img, cp_label_hongbao;
    private TextView cp_label_text_tx, cp_label_mic_tx, cp_label_img_tx, cp_label_hongbao_tx;
    private ImageView pailide_show_next;
    private ImageView pailide_show_share;
    int mictime;
    private int positint = 0;
    private YNStatus ynStatus;
    private BaseApplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_show_pailide);
        application = (BaseApplication) getApplication();
        registerBoradcastReceiver();
        initView();
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionReceiveInfo);
        myIntentFilter.addAction(ACTIONs.actionCloseactivity);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionCloseactivity:
                    finish();
                    break;
                case ACTIONs.actionReceiveInfo:
                    Log.i("xjs", "actionReceiveInfo===>");
                    YNStatus getynstatus = (YNStatus) intent.getSerializableExtra("ynstatus");
                    Log.i("xjs", "YNSingleton.getInstance().getStatusList().size1111===>" + YNSingleton.getInstance().getStatusList().size());
                    for (int i = 0; i < YNSingleton.getInstance().getStatusList().size(); i++) {
                        if (getynstatus.getEid() == YNSingleton.getInstance().getStatusList().get(i).getEid()) {
                            YNSingleton.getInstance().getStatusList().remove(i);
                        }
                    }
                    Log.i("xjs", "YNSingleton.getInstance().getStatusList().size2222222===>" + YNSingleton.getInstance().getStatusList().size());
                    YNSingleton.getInstance().getStatusList().add(getynstatus);
                    Log.i("xjs", "YNSingleton.getInstance().getStatusList().size3333333===>" + YNSingleton.getInstance().getStatusList().size());
                    positint = 0;
                    initdata();
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }


    private void initView() {
        mMediaPlayer = new MediaPlayer();
        findViewById(R.id.pailide_show_close).setOnClickListener(this);
        show_cover = (ImageView) findViewById(R.id.show_cover);
        time_pailide = (LinearLayout) findViewById(R.id.time_pailide);
        pailide_show_top = (RelativeLayout) findViewById(R.id.pailide_show_top);
        pailide_show_top.setLayoutParams(new LinearLayout.LayoutParams(UIHelper.getPailideShowHigh(this), UIHelper.getPailideShowHigh(this)));
        pailide_show_pic = (SimpleDraweeView) findViewById(R.id.pailide_show_pic);
        cp_label_text = (LinearLayout) findViewById(R.id.cp_label_text);
        cp_label_text.setOnClickListener(this);
        cp_label_text_tx = (TextView) findViewById(R.id.cp_label_text_tx);
        cp_label_mic = (LinearLayout) findViewById(R.id.cp_label_mic);
        cp_label_mic.setOnClickListener(this);
        cp_label_mic_tx = (TextView) findViewById(R.id.cp_label_mic_tx);
        cp_label_img = (LinearLayout) findViewById(R.id.cp_label_img);
        cp_label_img.setOnClickListener(this);
        cp_label_img_tx = (TextView) findViewById(R.id.cp_label_img_tx);
        cp_label_hongbao = (LinearLayout) findViewById(R.id.cp_label_hongbao);
        cp_label_hongbao.setOnClickListener(this);
        cp_label_hongbao_tx = (TextView) findViewById(R.id.cp_label_hongbao_tx);
        pailide_show_next = (ImageView) findViewById(R.id.pailide_show_next);
        pailide_show_share = (ImageView) findViewById(R.id.pailide_show_share);
        pailide_show_share.setOnClickListener(this);
        AlphaAnimation alphaAnimationbtns = new AlphaAnimation(0.0f, 1.0f);
        alphaAnimationbtns.setDuration(800);
        show_cover.startAnimation(alphaAnimationbtns);
        Animation translateAnimationf = new TranslateAnimation(0, 0, -UIHelper.dip2px(PailideShowActivity.this, 220), 0);
        translateAnimationf.setDuration(500);
        time_pailide.startAnimation(translateAnimationf);
        initdata();
    }

    private void initdata() {
        if (YNSingleton.getInstance().getStatusList().size() == 1) {
            pailide_show_next.setVisibility(View.GONE);
            ynStatus = YNSingleton.getInstance().getStatusList().get(0);
            ImageOptHelper.showFrescoStatus(ynStatus.geteVerifyPic(), pailide_show_pic, this);
            updateContent();
            updateMIc();
            updateImage();
            updateMoney();
        } else {
            pailide_show_next.setVisibility(View.VISIBLE);
            ynStatus = YNSingleton.getInstance().getStatusList().get(positint);
            ImageOptHelper.showFrescoStatus(ynStatus.geteVerifyPic(), pailide_show_pic, PailideShowActivity.this);
            updateContent();
            updateMIc();
            updateImage();
            updateMoney();
            pailide_show_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    positint++;
                    if (positint < YNSingleton.getInstance().getStatusList().size()) {
                        ynStatus = YNSingleton.getInstance().getStatusList().get(positint);
                    } else {
                        positint = 0;
                        ynStatus = YNSingleton.getInstance().getStatusList().get(positint);
                    }
                    ImageOptHelper.showFrescoStatus(ynStatus.geteVerifyPic(), pailide_show_pic, PailideShowActivity.this);
                    updateContent();
                    updateMIc();
                    updateImage();
                    updateMoney();
                }
            });
        }
    }

    @Override
    public void onBackPressed() {

    }


    private void updateMIc() {
        if (!StringUtils.isEmpty(ynStatus.getEaudio())) {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    try {
                        mMediaPlayer.setDataSource(ynStatus.getEaudio());
                        mMediaPlayer.prepare();
                    } catch (Exception e) {
                        e.printStackTrace();
                        return;
                    }
                }
            }.start();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mictime = mp.getDuration() / 1000;
                    cp_label_mic_tx.setText(TimeUtils.secToTime(mictime));
                    cp_label_mic.setVisibility(View.VISIBLE);
                }
            });
        } else {
            cp_label_mic_tx.setText("");
            cp_label_mic.setVisibility(View.GONE);
        }
    }


    private void updateImage() {
        if (ynStatus.getPicList().size() > 0) {
            cp_label_img_tx.setText("已添加" + ynStatus.getPicList().size() + "张照片");
            cp_label_img.setVisibility(View.VISIBLE);
        } else {
            cp_label_img_tx.setText("");
            cp_label_img.setVisibility(View.GONE);
        }
    }

    private void updateContent() {
        if (!StringUtils.isEmpty(ynStatus.getEtext())) {
            cp_label_text_tx.setText(ynStatus.getEtext());
            cp_label_text.setVisibility(View.VISIBLE);
        } else {
            cp_label_text_tx.setText("");
            cp_label_text.setVisibility(View.GONE);
        }
    }

    private void updateMoney() {
        if (ynStatus.getRedEnvelopInfo() != null) {
            cp_label_hongbao.setVisibility(View.VISIBLE);
        } else {
            cp_label_hongbao.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pailide_show_share:
                String url = "http://wx.zhuiyinanian.com/app/shareOpendHiddenEvent.html?code=";
                String sec = null;
                try {
                    sec = YNInviteSecret.encryptDES("eid=" + ynStatus.getEid(), getResources().getString(R.string.UNKNOW_ID));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                url = url + sec;
                Intent sintent = new Intent(this, LineTimeStatusShareActivity.class);
                sintent.putExtra("UrlShare", url);
                sintent.putExtra("title", "我分享了一张AR实景照片。");
                sintent.putExtra("desc", "朋友给我发了一张AR实景照片，我拿起手机扫了一下...");
                sintent.putExtra("avatarPaths", MineFragement.avatarPic);
                startActivity(sintent);
                break;
            case R.id.cp_label_text:
                startActivity(new Intent(this, PailideShowTextActivity.class).putExtra("text", ynStatus.getEtext()));
                break;
            case R.id.cp_label_mic:
                Intent intent = new Intent(this, MicPlayActivity.class);
                intent.putExtra("Eaudio", ynStatus.getEaudio());
                intent.putExtra("ePeopleName", ynStatus.getePeopleName());
                intent.putExtra("Eplace", ynStatus.getEplace());
                startActivity(intent);
                break;
            case R.id.cp_label_img:
                Intent Imgintent = new Intent(this, BrowseActivity.class);
                Imgintent.putExtra(CommonConstants.YNPOSITION, 0);
                Imgintent.putExtra(CommonConstants.TIMELINE, true);
                startActivity(Imgintent);
                break;
            case R.id.cp_label_hongbao:
                Log.i("xjs", "ynStatus.getRedEnvelopInfo().getRedEnvelopRemainNum()==>" + ynStatus.getRedEnvelopInfo().getRedEnvelopRemainNum());
                YNSingleton.getInstance().setCurStatus(ynStatus);
                if (ynStatus.getRedEnvelopInfo().getRedEnvelopRemainNum() == 0) {
                    Log.i("xjs", "rrrrrr");
                    Log.i("xjs", "ynStatus.getReceiveInfo().sss==>" + ynStatus.getReceiveInfo().size());
                    if (checkMeExist(ynStatus.getReceiveInfo())) {
                        startActivity(new Intent(this, PaiLidePaiHangActivity.class));
                    } else {
                        startActivity(new Intent(this, PaiLidePaiHangActivity.class).putExtra("redover", true));
                    }
                } else {
                    if (ynStatus.getReceiveInfo() != null && ynStatus.getReceiveInfo().size() > 0) {
                        if (checkMeExist(ynStatus.getReceiveInfo())) {
                            startActivity(new Intent(this, PaiLidePaiHangActivity.class));
                        } else {
                            startActivity(new Intent(this, PailideShowHongbaoActivity.class));
                        }
                    } else {
                        startActivity(new Intent(this, PailideShowHongbaoActivity.class));
                    }
                }

                break;
            case R.id.pailide_show_close:
                AlphaAnimation alphaAnimationbtns = new AlphaAnimation(1.0f, 0.0f);
                alphaAnimationbtns.setDuration(500);
                show_cover.startAnimation(alphaAnimationbtns);
                alphaAnimationbtns.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        show_cover.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                Animation translateAnimationf = new TranslateAnimation(0, 0, 0, UIHelper.dip2px(PailideShowActivity.this, 220));
                translateAnimationf.setDuration(500);
                time_pailide.startAnimation(translateAnimationf);
                translateAnimationf.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        time_pailide.setVisibility(View.GONE);
                        finish();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                break;
        }
    }

    private boolean checkMeExist(List<YNReceiverInfo> list) {
        for (int i = 0; i < list.size(); i++) {
            Log.i("xjs", "list.get(i).getUserid()==>" + list.get(i).getUserid());
            if (list.get(i).getUserid() == application.getCurID())
                return true;
        }
        return false;
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

}
